
#include <common/filehelper.hpp>

std::string FileDesc::read()
{
    char buffer[4096];
    ssize_t nbytes = ::read(m_fd, buffer, sizeof(buffer));

    if (nbytes == -1) {
        throw std::runtime_error("Failed to read from FIFO");
    } else if (nbytes == 0) {
        return std::string();
    } else {
        return std::string(buffer, nbytes);
    }
}

void FileDesc::write(const std::string &message)
{
    if (::write(m_fd, message.c_str(), message.size()) == -1) {
        throw std::runtime_error("Failed to write to FIFO");
    }
}
